package com.gree.scmservice.service.impl;

import com.gree.common.utils.StringUtils;
import com.gree.framework.entity.SysUserEx;
import com.gree.scmservice.service.GscmSysUserExService;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Created by 180686 on 2021/3/17 14:58
 */
@Service
public class GscmSysUserExServiceImpl implements GscmSysUserExService {

    //TODO 待完善Dao层
    @Autowired
    private Dao dao;

    private final String RESETPWD = "123456";
    private static final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

    @Override
    public int insertSysUserEx(SysUserEx sysUserEx) {
        String password = sysUserEx.getPwd();
        sysUserEx.setPwd(encoder.encode(password));
        sysUserEx.setCreateDt(LocalDateTime.now());
        SysUserEx sysUserExResult = dao.insert(sysUserEx);
        if(sysUserExResult != null){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public List<SysUserEx> selectSysUserExList(SysUserEx sysUserEx) {
        Cnd cnd = Cnd.NEW();
        if(StringUtils.isNotEmpty(sysUserEx.getUserName())){
            cnd = Cnd.NEW().where("username","like",sysUserEx.getUserName());
        }
        return dao.query(SysUserEx.class, cnd);
    }

    @Override
    public int updateSysUserEx(SysUserEx sysUserEx) {
        return dao.update(sysUserEx);
    }

    @Override
    public int deleteSysUserExByName(String username) {
        return dao.clear(SysUserEx.class,Cnd.where("username","=",username));
    }

    @Override
    public int resetPassword(String username) {
        return dao.update(SysUserEx.class, Chain.make("PWD",encoder.encode(RESETPWD)),Cnd.where("USERNAME","=",username));
    }

}
